﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Movies.Data;
using System;
using System.Linq;

namespace Movies.Models
{
    //设定数据库种子
    public static class SeedData
    {
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new MovieContext(serviceProvider.GetRequiredService<DbContextOptions<MovieContext>>()))
            {
                if (context.DbMovie.Any())
                {
                    return;
                }
                context.DbMovie.AddRange(
                    new Movie()
                    {
                        Title = "白夜行",
                        Genre = "剧情",
                        Price = 12.8M,
                        ReleaseDate = new DateTime(2021, 5, 24),
                    },
                    new Movie()
                    {
                        Title = "蜘蛛侠",
                        Genre = "科幻",
                        Price = 15.8M,
                        ReleaseDate = new DateTime(2021, 5, 24),
                    },
                    new Movie()
                    {
                        Title = "钢铁侠",
                        Genre = "科幻",
                        Price = 22.8M,
                        ReleaseDate = new DateTime(2021, 5, 24),
                    }
                    );
                context.SaveChanges();
            }
        }
    }
}
